package com.ddwl.goods.dao.bargain;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.Constant;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.mapper.bargain.BargainActivityMapper;
import com.ddwl.goods.model.Activity;
import com.ddwl.goods.model.bargain.BargainActivity;
import com.ddwl.schema.bo.goods.bargain.BargainActivityPageBo;
import com.ddwl.schema.bo.goods.bargain.BoostActivityPageBo;
import com.ddwl.schema.vo.goods.bargain.BargainActivityItemVo;
import com.ddwl.schema.vo.goods.bargain.BargainActivityVo;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2020/10/23 17:23
 */
@Repository
public class BargainActivityDao extends TkDao<BargainActivityMapper,BargainActivity> {

    @Resource
    private BargainActivityMapper bargainActivityMapper;

    public List<BargainActivityVo> getList(BargainActivityPageBo param) {
        BargainActivity activity = new BargainActivity();
        activity.setName(param.getName());
        activity.setStatus(param.getStatus());
        return bargainActivityMapper.getList(activity);
    }

    public List<BargainActivityItemVo> getBargainActivityItemList(String shopId) {
        return bargainActivityMapper.getBargainActivityItemList(shopId);
    }

    public List<BargainActivityVo> getMerchantList(BoostActivityPageBo param) {
        BargainActivity activity = new BargainActivity();
        activity.setStatus(param.getStatus());
        activity.setShopId(param.getShopId());
        return bargainActivityMapper.getList(activity);
    }

    public List<BargainActivityItemVo> boostList(List<String> shopIds) {
        if (CollectionUtils.isEmpty(shopIds)) {
            return new ArrayList<>();
        }
        return bargainActivityMapper.boostList(shopIds);
    }

    public void batchUpdateStock(List<BargainActivity> baList) {
        bargainActivityMapper.batchUpdateStock(baList);
    }

    public List<BargainActivity> findExpiredActivity() {
        Example example = getExample();
        example.and().andLessThanOrEqualTo("endTime", Instant.now())
                .andEqualTo("status", BaseEnum.StatusEnum.ENABLE.getCode());
        return bargainActivityMapper.selectByExample(example);
    }

    public void batchUpdateStatus(List<String> ids) {
        Example example = getExample();
        example.and().andIn("id", ids);
        BargainActivity ba = new BargainActivity();
        ba.setStatus(BaseEnum.StatusEnum.DISABLE.getCode());
        bargainActivityMapper.updateByExampleSelective(ba, example);
    }
}
